*** Replication file of Baqaee, Burstein and Koike-Mori (2022) *********************
*  Written by Yasutaka Koike-Mori (PhD student in economics at UCLA), 11/11/2022
*
*  Description: Create a price long-run time series for the UK that combines the CPI and RPI. 

clear
** CPI **********************
use "price\CPIdatabaseL.dta"
** rename *******************

************
* Food decomp : 1. BREADCEREAL: ok
*CPI: breadcereals
*RPI: bread	cereals	biscuits
rename cjwb cpiw_breadcereals
rename d7d5 cpi_breadcereals
 
rename czvo rpiw_bread
rename doaa rpi_bread
rename czvp rpiw_cereals
rename doab rpi_cereals
rename czvq rpiw_biscuits
rename doac rpi_biscuits

** aggregation
gen rpimw_BREADCEREAL = rpiw_bread + rpiw_cereals + rpiw_biscuits
qui foreach x of newlist bread	cereals	biscuits {
gen	rs_`x' = rpiw_`x'/rpimw_BREADCEREAL
}

gen rpim_BREADCEREAL = rs_bread*rpi_bread + rs_cereals*rpi_cereals +rs_biscuits*rpi_cereals
gen cpim_BREADCEREAL = cpi_breadcereals
************
* Food decomp : 2. Meat&Fish: ok
*CPI: Meat and Fish: meat	fish
*RPI: beef	lamb	pork	bacon	poultry	oth_meat	fish

rename cjwc cpiw_meat
rename d7d6 cpi_meat
rename cjwd cpiw_fish
rename d7d7 cpi_fish

rename czvr rpiw_beef
rename doad rpi_beef

rename hkic rpiw_lamb
rename doae rpi_lamb

rename czvu rpiw_pork
rename doag rpi_pork
rename czvv rpiw_bacon
rename doah rpi_bacon

rename czvw rpiw_poultry
rename doai rpi_poultry
rename czvx rpiw_oth_meat
rename doaj rpi_oth_meat

rename hkhk rpiw_fish
rename doak rpi_fish

gen rpimw_Meat_Fish = rpiw_beef + rpiw_lamb + rpiw_pork +	rpiw_bacon +	rpiw_poultry +	rpiw_oth_meat +	rpiw_fish
qui foreach x of newlist beef	lamb	pork	bacon	poultry	oth_meat	fish {
gen	rs_`x' = rpiw_`x'/rpimw_Meat_Fish
}
gen rpim_Meat_Fish = rs_beef*rpi_beef  + rs_lamb*rpi_lamb + rs_pork*rpi_pork +	rs_bacon*rpi_bacon +	rs_poultry*rpi_poultry +	rs_oth_meat*rpi_oth_meat +	rs_fish*rpi_fish

gen cpimw_Meat_Fish = cpiw_meat + cpiw_fish
qui foreach x of newlist meat	fish {
gen	cs_`x' = cpiw_`x'/cpimw_Meat_Fish
}
gen cpim_Meat_Fish = cs_meat*cpi_meat + cs_fish*cpi_fish


**3. Milk+Eggs: ok
*CPI: milkcheeseeggs
*RPI: butter		cheese	eggs	milkfres	milkprod

rename cjwe cpiw_milkcheeseeggs
rename d7d8 cpi_milkcheeseeggs

rename czwa rpiw_butter
rename doam rpi_butter
rename czwc rpiw_cheese
rename doao rpi_cheese

rename czwd rpiw_eggs
rename doap rpi_eggs
rename czwe rpiw_milkfres
rename doaq rpi_milkfres

rename czwf rpiw_milkprod
rename doar rpi_milkprod

gen rpimw_Milk_Eggs = rpiw_butter+ rpiw_cheese+ rpiw_eggs+ rpiw_milkfres+ rpiw_milkprod
qui foreach x of newlist butter	cheese	eggs	milkfres	milkprod {
gen	rs_`x' = rpiw_`x'/rpimw_Milk_Eggs
}
gen rpim_Milk_Eggs = rs_butter*rpi_butter  + rs_cheese*rpi_cheese + rs_eggs*rpi_eggs +	rs_milkfres*rpi_milkfres +	rs_milkprod*rpi_milkprod
gen cpim_Milk_Eggs = cpi_milkcheeseeggs

**4. oilfats
*CPI: oilfats
*RPI: oil_fats

rename cjwf cpiw_oilfats
rename d7d9 cpi_oilfats

rename czwb rpiw_oilfats
rename doan rpi_oilfats

gen rpim_Oilfats = rpi_oilfats
gen cpim_Oilfats = cpi_oilfats


*CPI: 5. fruit: ok 
*RPI: fruit

rename cjwg cpiw_fruit
rename d7da cpi_fruit

rename hkig rpiw_fruit
rename dobb rpi_fruit

gen rpim_Fruit = rpi_fruit
gen cpim_Fruit = cpi_fruit

**6. vegetables
*CPI: vegetables
*RPI: potatoes	oth_vegs

rename cjwh cpiw_vegetables
rename d7db cpi_vegetables

rename hkie rpiw_potatoes
rename doax rpi_potatoes
rename hkif rpiw_oth_vegs
rename doba rpi_oth_vegs

gen rpimw_vegetables = rpiw_potatoes + rpiw_oth_vegs
qui foreach x of newlist potatoes oth_vegs{
gen	rs_`x' = rpiw_`x'/rpimw_vegetables
}
gen rpim_Vegetables = rs_potatoes*rpi_potatoes  + rs_oth_vegs*rpi_oth_vegs
gen cpim_Vegetables = cpi_vegetables


* 7. otherfood
*CPI: sugar	foodproducts
*RPI: swe_choc sug_pres oth_food

rename cjwi cpiw_sugar
rename d7dc cpi_sugar
rename cjwj cpiw_foodproducts
rename d7dd cpi_foodproducts

rename czwk rpiw_swe_choc
rename doaw rpi_swe_choc
rename czwj rpiw_sug_pres
rename doav rpi_sug_pres
rename czwr rpiw_oth_food
rename dobd rpi_oth_food

gen rpimw_otherfood = rpiw_swe_choc + rpiw_sug_pres + rpiw_oth_food
qui foreach x of newlist swe_choc sug_pres oth_food {
gen	rs_`x' = rpiw_`x'/rpimw_otherfood
}
gen rpim_otherfood = rs_swe_choc*rpi_swe_choc  + rs_sug_pres*rpi_sug_pres + rs_oth_food*rpi_oth_food

gen cpimw_otherfood = cpiw_sugar + cpiw_foodproducts
qui foreach x of newlist sugar	foodproducts {
gen	cs_`x' = cpiw_`x'/cpimw_otherfood
}
gen cpim_otherfood = cs_sugar*cpi_sugar  + cs_foodproducts*cpi_foodproducts



* Food decomp :8. SOFTDRINK: ok
* RPI: tea	coffee	softdrin
* CPI: SOFTDRINK
rename cjuy cpiw_softdrink
rename d7c9 cpi_softdrink

rename czwg rpiw_tea
rename doas rpi_tea
rename czwh rpiw_coffee
rename doat rpi_coffee
rename czwi rpiw_softdrink
rename doau rpi_softdrink
** aggregation
gen rpimw_SOFTDRINK = rpiw_tea + rpiw_coffee + rpiw_softdrink
qui foreach x of newlist tea coffee softdrink {
gen	rs_`x' = rpiw_`x'/rpimw_SOFTDRINK
}
gen rpim_SOFTDRINK = rs_tea*rpi_tea + rs_coffee*rpi_coffee + rs_softdrink*rpi_softdrink
gen cpim_SOFTDRINK = cpi_softdrink

* 9. TOBACCO: ok
gen cpiw_TOBACCO = cjwp
gen cpi_TOBACCO =  d7cb
gen rpiw_TOBACCO = czhe 
gen rpi_TOBACCO = chbe 

gen rpim_TOBACCO = rpi_TOBACCO
gen cpim_TOBACCO = cpi_TOBACCO

* 10. restrauant (CATERING) + ALCOHOL: ok
*CPI: ALCOHOL
*RPI: ALCOHOL CATERING

gen cpiw_CATERINGSERVS = cjvu 
gen cpi_CATERINGSERVS = d7cw 

rename cjuz cpiw_alcohol
rename d7ca cpi_alcohol

gen rpiw_CATERING = czhc
gen rpi_CATERING = chbc
gen rpiw_alcohol = czhd 
gen rpi_alcohol = chbd 

gen rpimw_CATERING = rpiw_alcohol + rpiw_CATERING
qui foreach x of newlist alcohol CATERING{
gen	rs_`x' = rpiw_`x'/rpimw_CATERING 
}
gen rpim_CATERING  = rs_alcohol*rpi_alcohol+ rs_CATERING*rpi_CATERING

gen cpimw_CATERING = cpiw_alcohol + cpiw_CATERINGSERVS
qui foreach x of newlist alcohol CATERINGSERVS{
gen	cs_`x' = cpiw_`x'/cpimw_CATERING 
}
gen cpim_CATERING  = cs_alcohol*cpi_alcohol+ cs_CATERINGSERVS*cpi_CATERINGSERVS


**11. housing: water is included in housing in RPI: ok
*CPI: HOUSING - FUEL
*RPI: HOUSING - insgr
*rename chzu cpiw_Housing
*rename d7bx cpi_Housing
gen cpiw_Housing = chzu 
gen cpi_Housing = d7bx


gen rpiw_housing = czhf 
gen rpi_housing = chbf 
gen rpiw_FUEL_LIG = czhg 
gen rpi_FUEL_LIG = chbg 
rename dobv rpi_insr
rename czxj rpiw_insr 
rename dobq rpi_mort
rename czgy rpiw_mort
replace rpiw_mort = 1000- rpiw_mort


gen rpimw_Housing = rpiw_housing + rpiw_FUEL_LIG - rpiw_insr  - rpiw_mort
qui foreach x of newlist housing FUEL_LIG insr mort{
gen	rs_`x' = rpiw_`x'/rpimw_Housing
}
gen rpim_Housing  = rs_housing*rpi_housing + rs_FUEL_LIG*rpi_FUEL_LIG - rs_insr*rpi_insr  - rs_mort*rpi_mort


gen cpim_Housing  = cpi_Housing

**12. Clothing

gen rpim_Clothing = chbj 
gen cpim_Clothing = d7bw 


** 13. Household Goods & service: ok
*RPI: HHGOODS domservs 
*CPI: FURNEQUIP (including domservices)  

gen rpiw_HHG = czhh 
gen rpi_HHG = chbh 

gen rpiw_domservs  = czxw
gen rpi_domservs  = doci 
gen rpiw_pet  = czxt
gen rpi_pet  = docf 

gen rpimw_HHGS = rpiw_HHG + rpiw_domservs
qui foreach x of newlist HHG domservs {
gen	rs_`x' = rpiw_`x'/rpimw_HHGS
}
gen rpim_HHGS  = rs_HHG*rpi_HHG + rs_domservs*rpi_domservs 



gen cpi_FURNEQUIP = d7by 
gen cpiw_FURNEQUIP = chzv 
gen cpim_HHGS  = cpi_FURNEQUIP

** 14. Communication: ok
*RPI: postage	telephon	telemess
*CPI: COMMUNICATION

gen rpiw_postage = czxu
gen rpi_postage = docg 
gen rpiw_telephon = czxv
gen rpi_telephon = doch

gen rpimw_Communication = rpiw_postage + rpiw_telephon
qui foreach x of newlist postage telephon{
gen	rs_`x' = rpiw_`x'/rpimw_Communication
}
gen rpim_Communication = rs_postage*rpi_postage + rs_telephon*rpi_telephon
gen cpim_Communication = d7c3


* 15. Personal goods and service: PG_S :ok
* RPI: P_GD_SER fees_sub insr	
* CPI: HEALTH, MISC

gen rpiw_P_GD_SER = czhk
gen rpi_P_GD_SER = chbq

gen rpiw_fees_sub = czxx
gen rpi_fees_sub = docj

drop rs_*

gen rpimw_PG_S  = rpiw_P_GD_SER + rpiw_fees_sub + rpiw_insr
qui foreach x of newlist P_GD_SER fees_sub insr{
gen	rs_`x' = rpiw_`x'/rpimw_PG_S
}
gen rpim_PG_S = rs_P_GD_SER*rpi_P_GD_SER + rs_fees_sub*rpi_fees_sub  + rs_insr*rpi_insr

gen cpiw_HEALTH = chzw 
gen cpi_HEALTH = d7bz

gen cpiw_MISC = cjuw
gen cpi_MISC = d7c7

gen cpimw_PG_S = cpiw_HEALTH + cpiw_MISC
qui foreach x of newlist HEALTH MISC{
gen	cs_`x' = cpiw_`x'/cpimw_PG_S
}
gen cpim_PG_S = cs_HEALTH*cpi_HEALTH + cs_MISC*cpi_MISC 


**************:
*16. Transport : ok
* RPI: MOTORING FARESET
* CPI: TRANSPORT
gen rpiw_MOTORING = czhl
gen rpi_MOTORING = chbk

gen rpiw_FARESET = czhm 
gen rpi_FARESET = chbr

gen rpimw_Transport  = rpiw_MOTORING + rpiw_FARESET
qui foreach x of newlist MOTORING FARESET{
gen	rs_`x' = rpiw_`x'/rpimw_Transport
}
gen rpim_Transport = rs_MOTORING*rpi_MOTORING + rs_FARESET*rpi_FARESET

gen cpim_Transport = d7c2

** 17 LGS: ok
*RPI: LGOODS + LSERVS + pet. 
*CPI: Recreation + EDUCATION + ACCOMSERVICES
gen rpiw_LGOODS = czhn
gen rpi_LGOODS = chbl
gen rpiw_LSERVS = czho
gen rpi_LSERVS = chbm

*gen rpiw_pet  = CZXT
*gen rpi_pet  = DOCF

gen rpimw_LGS  = rpiw_LGOODS + rpiw_LSERVS 
qui foreach x of newlist LGOODS LSERVS {
gen	rs_`x' = rpiw_`x'/rpimw_LGS
}
gen rpim_LGS = rs_LGOODS*rpi_LGOODS + rs_LSERVS*rpi_LSERVS 


gen cpiw_Recreation = chzz
gen cpi_Recreation = d7c4
gen cpiw_EDUCATION = cjuu
gen cpi_EDUCATION = d7c5
gen cpiw_ACCOMSERVICES = cjvv
gen cpi_ACCOMSERVICES = d7cx

gen cpimw_LGS  = cpiw_Recreation + cpiw_EDUCATION + cpiw_ACCOMSERVICES
qui foreach x of newlist Recreation EDUCATION ACCOMSERVICES{
gen	cs_`x' = cpiw_`x'/cpimw_LGS
}
gen cpim_LGS = cs_Recreation*cpi_Recreation + cs_EDUCATION*cpi_EDUCATION + cs_ACCOMSERVICES*cpi_ACCOMSERVICES


**CPI

local num 1
foreach x of newlist BREADCEREAL Meat_Fish Milk_Eggs Oilfats Fruit Vegetables otherfood SOFTDRINK TOBACCO CATERING Housing Clothing HHGS Communication PG_S Transport LGS  {
rename cpim_`x' cpim`num'
local num = `num' + 1	
}

preserve
keep if strlen(datayear) == 4
destring datayear,replace
drop if datayear < 1988
drop if datayear > 2017
keep cpim1 cpim2 cpim3 cpim4 cpim5 cpim6 cpim7 cpim8 cpim9 cpim10 cpim11 cpim12 cpim13 cpim14 cpim15 cpim16 cpim17 datayear
*line cpim* datayear
save "processed\cpimerge_dis.dta",replace
restore 

**RPI************


keep if strlen(datayear) == 4
destring datayear,replace
drop if datayear < 1987
drop if datayear > 2017
*append using "price\RPIdatabaseArchive.dta"
sort datayear

local num 1
foreach x of newlist BREADCEREAL Meat_Fish Milk_Eggs Oilfats Fruit Vegetables otherfood SOFTDRINK TOBACCO CATERING Housing Clothing HHGS Communication PG_S Transport LGS {
rename rpim_`x' rpim`num'
local num = `num' + 1	
}
*merge m:1 datayear using "price\RPIdatabaseArchive_dis.dta"
*keep if strlen(datayear) == 4
destring datayear,replace
drop if datayear < 1974
drop if datayear > 2017
keep rpim1 rpim2 rpim3 rpim4 rpim5 rpim6 rpim7 rpim8 rpim9 rpim10 rpim11 rpim12 rpim13 rpim14 rpim15 rpim16 rpim17 datayear
*line cpim* datayear
save "processed\rpimerge_dis.dta",replace

merge m:1 datayear using "processed\cpimerge_dis.dta"


forvalues x = 1/17	 {
	gen tmp`x' = 0
	replace tmp`x' = rpim`x'/cpim`x' if datayear == 1997
	egen base`x' = total(tmp`x')	
	
gen price`x' = rpim`x' if datayear < 1998
replace price`x' = cpim`x'*base`x' if datayear > 1997
}
keep price* datayear

append using "price\RPIdatabaseArchive_dis.dta"
 sort datayear
save "processed\pricemerge_dis.dta",replace

preserve 
drop datayear
outsheet using DataforMatlab\price.csv, comma replace nonames
restore
***************************************Information**********************************************

/*
** households goods:
CPI: pets + FURNEQUIP
RPI: HHGOODS

** HHSERVS
P_GD_SER

CPI: domservices + PHONE + ????
RPI: HHSERVS

* mortegins 
CPI: TRANSPORT + transinsurance
RPI: MOTORING + FARESET

* Leisure Goods & Service
CPI: books	newspapers RECDURABLES	(OTHERRECGOODS - pets) 
    instead = (NEWSBOOKS)
RPI: Leisure goods

* Leisure services
CPI: cultureservices + PKGHOLIDAYS + RECSERVS + ACCOMSERVICES

RPI: LSERVS
*/




**Food1	Catering2	Alcoholic drink3	Tobacco4	Housing5	Fuel & light6	Clothing & footwear7
**Motoring8 & other travel9	Household goods10	Household services11	Personal goods & services12	Leisure goods13	Leisure services14
*price
*CHAW  CHBA CHBC CHBD CHBE CHBF CHBG CHBJ CHBK CHBR CHBH CHBI CHBQ CHBL CHBM   DOBV DOBQ
*weight
*CZGZ CZHC CZHD CZHE CZHF CZHG CZHJ CZHL CZHM CZHH CZHI CZHK CZHN CZHO  CZXJ CZGY

/*

*RPI
/*
*/
/
**
RPI: Total food (Jan 1987=100)
CHBA
RPI:Weights (parts per 1000) - Food
CZGZ
**
RPI: Catering (Jan 1987=100)
CHBC
RPI:Weights (parts per 1000) - Catering 
CZHC
**
RPI: Alcoholic drink (Jan 1987=100)
CHBD
RPI:Weights (parts per 1000) - Alcoholic drink 
CZHD
**
RPI: Cigarettes & tobacco (Jan 1987=100)  
CHBE
RPI:Weights (parts per 1000) - Tobacco 
CZHE
**
RPI: Housing (Jan 1987=100) 
CHBF
RPI:Weights (parts per 1000) - Housing 
CZHF





**
RPI: Fuel & light (Jan 1987=100) 
CHBG
RPI:Weights (parts per 1000) - Fuel and light 
CZHG
**
RPI: Clothing & footwear (Jan 1987=100)
CHBJ
RPI:Weights (parts per 1000) - Clothing and footwear 
CZHJ
**
RPI: Motoring expenditure (Jan 1987=100) 
CHBK
RPI:Weights (parts per 1000) - Motoring expenditure 
CZHL
***
RPI: Fares & other travel costs (Jan 1987=100) 
CHBR
RPI:Weights (parts per 1000) - Fares and other travel costs 
CZHM
**
RPI: Household goods (Jan 1987=100)   
CHBH
RPI:Weights (parts per 1000) - Household goods
CZHH 
**
RPI: Household services (Jan 1987=100)
CHBI
RPI:Weights (parts per 1000) - Household services
CZHI
**
RPI: Personal goods & services (Jan 1987=100)
CHBQ
RPI:Weights (parts per 1000) - Personal goods and services
CZHK
**
RPI: Leisure goods (Jan 1987=100) 
CHBL
RPI:Weights (parts per 1000) - Leisure goods
CZHN
**
RPI: Leisure services (Jan 1987=100) 
CHBM
RPI:Weights (parts per 1000) - Leisure services
CZHO
***
Extra: 
**
RPI: housing: mortgage interest payments (Jan 1987=100)
DOBQ
RPI:Weights (parts per 1000) - All items except mortgage interest payments
CZGY

***
RPI: housing: dwelling insurance & ground rent (Jan 1987=100)
DOBV
RPI:Weights (parts per 1000) - Dwelling insurance & ground rent
CZXJ

RPI All Items Index: Jan 1987=100
CHAW

**non use
RPI: housing: council tax & rates (Jan 1987=100)
DOBR
RPI: housing: depreciation(Jan 1995=100)
CHOO
******
RPI: all items exc. morgage interest payments and depreciation (Jan 1987=100)
CHON (1000-*)

**
CPI INDEX 00: ALL ITEMS 2015=100
D7BT
CPIH INDEX 00: ALL ITEMS 2015=100
L522
**
*/


**1 Food: No merge
**2 TOBACCO: TOBACCO | TOBACCO&Alcohol - Alcohol
**3 CLOTHING: No merge
**4 Housing_Fuel: Housing + Fuel | Houseing&Fuel
**5 HHG: HHG | FURN: No merge
**6 PGS: Personal goods & services | HEALTH + MISCELLANEOUS  
**7 TRANSPORT: TRANSPORT + Fareset | Motoring & other travel
**8 LGS: Leisure goods + Leisure services | COMMUNICATION + Recreation + EDUCATION + ACCOMSERVICES
**9 Catering: Catering | HOTELS, CAFES AND RESTAURANTS - ACCOMSERVICES + Alcohol

** CPI to RPI************
** COMMUNICATION: Leisure goods
** HEALTH: Personal Goods & Services
** Recreation + EDUCATION: Leisure Services

** Catering(RPI)はHOTELS, CAFES AND RESTAURANTSに含まれる
* HOTELS(CPI)は Leisure Services(RPI)に含まれる

/*
CPI INDEX 01 : FOOD AND NON-ALCOHOLIC BEVERAGES 2015=100
D7BU CHZR
CPI INDEX 02:ALCOHOLIC BEVERAGES,TOBACCO & NARCOTICS 2015=100
D7BV CHZS
CPI INDEX 03 : CLOTHING AND FOOTWEAR 2015=100
D7BW CHZT
CPI INDEX 04 : HOUSING, WATER AND FUELS 2015=100
D7BX CHZU
CPI INDEX 05 : FURN, HH EQUIP & ROUTINE REPAIR OF HOUSE 2015=100 DIY 
D7BY CHZV
CPI INDEX 06 : HEALTH 2015=100
D7BZ CHZW
CPI INDEX 07 : TRANSPORT 2015=100
D7C2 CHZX 
CPI INDEX 08 : COMMUNICATION 2015=100
D7C3 CHZY
CPI INDEX 09 : RECREATION & CULTURE 2015=100
D7C4 CHZZ
CPI INDEX 10 : EDUCATION 2015=100
D7C5 CJUU
CPI INDEX 11 : HOTELS, CAFES AND RESTAURANTS 2015=100
D7C6 CJUV
CPI INDEX 12 : MISCELLANEOUS GOODS AND SERVICES 2015=100
D7C7 CJUW
CPI INDEX 00: ALL ITEMS 2015=100
D7BT
**Extra
	CPI INDEX 11.2 : ACCOMMODATION SERVICES 2015=100
	D7CX CJVV
	CPI INDEX 02.1 : ALCOHOLIC BEVERAGES 2015=100
	D7CA CJUZ
	CPI INDEX 02.2 : TOBACCO 2015=100
	D7CB CJWP
	CPI INDEX 11.1 : Catering services 2015=100
	D7CW CJVU

*/


